package com.example.shop_2304.fg

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.fragment.findNavController
import androidx.navigation.ui.setupWithNavController
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseFragment
import com.example.base.mvvm.BaseViewFragment
import com.example.shop_2304.R
import com.example.shop_2304.databinding.FragmentMineBinding
import com.example.shop_2304.login.LoginViewModel
import com.example.shop_2304.utils.GlideUtils
import com.github.gzuliyujiang.wheelpicker.AddressPicker
import com.github.gzuliyujiang.wheelpicker.DatePicker
import com.github.gzuliyujiang.wheelpicker.OptionPicker
import com.github.gzuliyujiang.wheelpicker.annotation.AddressMode
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File


class MineFragment : BaseFragment<FragmentMineBinding,LoginViewModel>() {
    override fun initView() {
        //binding.bottom.setupWithNavController(requireActivity().findNavController(R.id.container2))

        val navHostFragment =
            childFragmentManager.findFragmentById(R.id.container2) as NavHostFragment
        val navController = navHostFragment.navController
        binding.bottom.setupWithNavController(navController)
        //修改性别
        binding.sex.setOnClickListener {
            val list = mutableListOf<String>("男","女","不详")
            val optionPicker = OptionPicker(requireActivity())
            optionPicker.setData(list)
            optionPicker.setOnOptionPickedListener { position, item ->
                binding.sex.text = list[position]
            }
            optionPicker.show()
        }
        //点击修改地址
        binding.address.setOnClickListener {
            val addressPicker = AddressPicker(requireActivity())
            addressPicker.setAddressMode(AddressMode.PROVINCE_CITY_COUNTY)
            addressPicker.setOnAddressPickedListener { province, city, county ->
                binding.address.text = "${province.name}${city.name}${county.name}"
            }
            addressPicker.show()
        }
        //修改出生年月日
        binding.birthday.setOnClickListener {
            val datePicker = DatePicker(requireActivity())
            datePicker.wheelLayout.setRange(DateEntity.target(1900,1,1), DateEntity.today(),
                DateEntity.today())
            datePicker.setOnDatePickedListener { year, month, day ->
                binding.birthday.text = "$year-$month-$day"
            }
            datePicker.show()
        }

        binding.iv.setOnClickListener {
            PictureSelector.create(this,200).selectPicture()
        }
    }

    override fun initData() {
        viewModel.uploadSuccess.observe(this){
            ToastUtils.showLong(it.message)
        }
    }

    override fun getLayout(): Int = R.layout.fragment_mine
    override fun getViewModel(): Class<LoginViewModel>  = LoginViewModel::class.java

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode==200&&resultCode==Activity.RESULT_OK){
            val pictureBean = data?.extras?.get(PictureSelector.PICTURE_RESULT) as PictureBean
            context?.let { GlideUtils.loadCircleImage(it,pictureBean.path,binding.iv) }
            //上传到服务器上
            val body = RequestBody.create("multipart/form-data".toMediaTypeOrNull(), File(pictureBean.path))
            val part = MultipartBody.Part.createFormData("multipart","aa.jpg",body)
            viewModel.upload("user",part,"0");
        }
    }

}